import {
  RebateModel
} from '../../../../models/rebate'
import { formatDate } from '../../../../utils/index'
const App =  getApp();

const rebateModel = new RebateModel()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    winHeight: "",
    vipUserFansList:null,
    startDate: '',
    endDate: '',
    today: '',
    pageIndex: 1,
    pageSize: 10,
    loading: false,
    hasNext: true,
    empty:false,
    statusBarHeight: App.globalData.statusBarHeight,
    isTabbar:false,
    active: 1,
    dayDataList: null,
    achievementsAll: '',
    rebateAll: ''
  },

  // 开始时间
  bindStartDateChange: function(e) {
    this.setData({
      startDate: e.detail.value
    })
    this.setData({
      pageIndex:1, 
      pageSize: 10
    })
    this.initData()
  },
  
  // 结束时间
  bindEndDateChange: function(e) {
    this.setData({
      endDate: e.detail.value
    })
    this.setData({
      pageIndex:1, 
      pageSize: 10
    })
    this.initData()
  },
  getRebateDefault: function () {
    this.data.pageIndex = 1
    const params = {
      startDate: this.data.startDate,
      endDate: this.data.endDate,
      pageIndex: this.data.pageIndex,
      pageSize: this.data.pageSize,
    }
    const self = this;
    rebateModel.queryRebateRanking(params).then(res => {
      if (res.data.vipUserFansList.length == 0) {
        self.data.empty = true
      } else {
        self.data.empty = false
      }
      self.setData({
        empty:self.data.empty,
        vipUserFansList: res.data.vipUserFansList,
        hasNext: res.data.hasNext,
        loading: false
      })
    })
  },

  // 日绩效
  getDayAchievements() {
    this.data.pageIndex = 1
    const params = {
      startDate: this.data.startDate,
      endDate: this.data.endDate,
      pageIndex: this.data.pageIndex,
      pageSize: this.data.pageSize,
    }
    const self = this;
    rebateModel.queryDayAchievements(params).then(res => {
      if (res.data.dayDataList.length == 0) {
        self.data.empty = true
      } else {
        self.data.empty = false
      }
      self.setData({
        empty:self.data.empty,
        dayDataList: res.data.dayDataList,
        rebateAll: res.data.totalData.rebateAll,
        achievementsAll: res.data.totalData.achievementsAll,
        hasNext: res.data.hasNext,
        loading: false
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.active){
      this.setData({
        active: options.active
      })
    }
  },

  initData:function(){
    if (this.data.active==1) {
      this.getRebateDefault()
    } else if(this.data.active == 2) {
      this.getDayAchievements()
    }
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (!this.data.startDate) {
      this.setData({
        startDate: formatDate((new Date()).getTime(), 'yyyy-MM-dd')
      })
    }
    if (!this.data.endDate) {
      this.setData({
        endDate: formatDate((new Date()).getTime(), 'yyyy-MM-dd')
      })
    }
    this.setData({
      today: formatDate((new Date()).getTime(), 'yyyy-MM-dd')
    })
    this.initData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      startDate: formatDate((new Date()).getTime(), 'yyyy-MM-dd'),
      endDate: formatDate((new Date()).getTime(), 'yyyy-MM-dd'),
      pageIndex:1, 
      pageSize: 10
    })
    this.initData()
    wx.stopPullDownRefresh()
  },

  //加载更多
  loadMore(isPull) {
    if (this.data.loading) {
      return
    }
    if (this.data.hasNext) {
      const params = {
        startDate: this.data.startDate,
        endDate: this.data.endDate,
        pageIndex: this.data.pageIndex,
        pageSize: this.data.pageSize,
      }
      if (isPull) {
        this.setData({
          loading: true
        })
      }
      if (this.data.active == 1) {
        rebateModel.queryRebateRanking(params).then(res => {
          const tempArray = this.data.vipUserFansList.concat(res.data.vipUserFansList)
          this.setData({
            vipUserFansList: tempArray,
            hasNext: res.data.hasNext,
            loading: false
          })
        })
      } else if (this.data.active == 2) {
        rebateModel.queryDayAchievements(params).then(res => {
          const tempArray = this.data.dayDataList.concat(res.data.dayDataList)
          this.setData({
            dayDataList: tempArray,
            rebateAll: res.data.totalData.rebateAll,
            achievementsAll: res.data.totalData.achievementsAll,
            hasNext: res.data.hasNext,
            loading: false
          })
        })
      }
      
    }
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(!this.data.hasNext){
      return
    }
    this.data.pageIndex = this.data.pageIndex + 1
    this.loadMore(true)
  },

})